package com.pg.controller.dd;

import com.pg.entity.dd.OrderView;
import com.pg.entity.rz.EipDataAcquisitionInfo;
import com.pg.entity.rz.EipDataSynchronizationInfo;
import com.pg.service.*;
import com.pg.utils.DateFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 订单前端控制器
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/index")
//@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CjsjService cJsjService;

    @Autowired
    private SysjService sysjService;

    @Autowired
    private EipSalesOrderService eipSalesOrderService;

    @Autowired
    private EipProductionOrderService eipProductionOrderService;

    @Autowired
    private EipWorkOrderService eipWorkOrderService;

//    @Autowired
//    private EipOperationService eipOperationService;

    @Autowired
    private EipDataAcquisitionInfoService eipDataAcquisitionInfoService;

    @Autowired
    private EipDataSynchronizationInfoService eipDataSynchronizationInfoService;


    /**
     * 查询推送订单集合
     *
     * @return 是否成功
     */
    @ApiOperation("查询推送订单集合")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/acquisitionOrdersList")
    public Result<?> selectAcquisitionOrdersList() {
        Calendar calendar = Calendar.getInstance();
        // 得到前一天
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        String modifyTime = DateFormat.date2String2(time).substring(0, 8);

        //查询数据库
        Map<String, String> map = new HashMap<>();
        map.put("modifyTime", modifyTime);
        List<OrderView> salesOrderList = eipSalesOrderService.selectListByMap(map);
        List<OrderView> productionOrderList = eipProductionOrderService.selectListByMap(map);
        List<OrderView> workOrderList = eipWorkOrderService.selectListByMap(map);
//        List<OrderView> operationList = eipOperationService.selectListByMap(map);

        List<OrderView> orderViewsList = new ArrayList<>();
        orderViewsList.addAll(salesOrderList);
        orderViewsList.addAll(productionOrderList);
        orderViewsList.addAll(workOrderList);
//        orderViewsList.addAll(operationList);
        return Result.ok(orderViewsList);
    }

    /**
     * 查询同步订单集合
     *
     * @return 是否成功
     */
    @ApiOperation("查询同步订单集合")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/synchronizationOrdersList")
    public Result<?> selectSynchronizationOrdersList() {
        Calendar calendar = Calendar.getInstance();
        // 得到前一天
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        String modifyTime = DateFormat.date2String2(time).substring(0, 8);


        //查询数据库
        Map<String, String> map = new HashMap<>();
        map.put("modifyTime", modifyTime);
        map.put("ioflag", "1");
        List<OrderView> salesOrderList = eipSalesOrderService.selectListByMap(map);
        List<OrderView> productionOrderList = eipProductionOrderService.selectListByMap(map);
        List<OrderView> workOrderList = eipWorkOrderService.selectListByMap(map);
//        List<OrderView> operationList = eipOperationService.selectListByMap(map);

        List<OrderView> orderViewsList = new ArrayList<>();
        orderViewsList.addAll(salesOrderList);
        orderViewsList.addAll(productionOrderList);
        orderViewsList.addAll(workOrderList);
//        orderViewsList.addAll(operationList);
        return Result.ok(orderViewsList);
    }

    /**
     * 查询订单数据
     *
     * @param startTime 起始日期
     * @param endTime   结束日期
     * @return
     */
    @ApiOperation("查询订单数据")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/ordersList")
    public Result<?> selectOrdersList(
            @RequestParam(name = "startTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(name = "endTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        HashMap<String, Object> pzZhdqOrderMap = new HashMap<>();
        pzZhdqOrderMap.put("supplierCode", "1000014563");
        pzZhdqOrderMap.put("subclassCode", "40001");
        pzZhdqOrderMap.put("startTime", startTime);
        pzZhdqOrderMap.put("endTime", endTime);
        int pzZhdqCount = orderService.selectDdCountByMap(pzZhdqOrderMap);

        HashMap<String, Object> pgZhdqOrderMap = new HashMap<>();
        pgZhdqOrderMap.put("supplierCode", "1000015019");
        pgZhdqOrderMap.put("subclassCode", "40001");
        pgZhdqOrderMap.put("startTime", startTime);
        pgZhdqOrderMap.put("endTime", endTime);
        int pgZhdqCount = orderService.selectDdCountByMap(pgZhdqOrderMap);

        HashMap<String, Object> pgDlqOrderMap = new HashMap<>();
        pgDlqOrderMap.put("supplierCode", "1000015019");
        pgDlqOrderMap.put("subclassCode", "40002");
        pgDlqOrderMap.put("startTime", startTime);
        pgDlqOrderMap.put("endTime", endTime);
        int pgDlqCount = orderService.selectDdCountByMap(pgDlqOrderMap);

        HashMap<String, Object> pgCgyOrderMap = new HashMap<>();
        pgCgyOrderMap.put("supplierCode", "1000015019");
        pgCgyOrderMap.put("subclassCode", "40011");
        pgCgyOrderMap.put("startTime", startTime);
        pgCgyOrderMap.put("endTime", endTime);
        int pgCgyCount = orderService.selectDdCountByMap(pgCgyOrderMap);
        Map<String, Number> data = new HashMap<>();
        data.put("pzgs", pzZhdqCount);
        data.put("zhdq", pgZhdqCount);
        data.put("cksyb", pgDlqCount + pgCgyCount);
        return Result.ok(data);
    }

    /**
     * 查询采集数据
     *
     * @param startTime 起始日期
     * @param endTime   结束日期
     * @return 是否成功
     */
    @ApiOperation("查询采集数据")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/cjsjList")
    public Result<?> selectCjsjList(
            @RequestParam(name = "startTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(name = "endTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        // 平芝组合电器烘干间温度
        HashMap<String, Object> pzZhdqHgjMap = new HashMap<>();
        pzZhdqHgjMap.put("supplierCode", "1000014563");
        pzZhdqHgjMap.put("categoryCode", "40001");
        pzZhdqHgjMap.put("startTime", startTime);
        pzZhdqHgjMap.put("endTime", endTime);
        int pzZhdqHgjNum = cJsjService.hgjCount(pzZhdqHgjMap);
        // 平高组合电器烘干间温度
        HashMap<String, Object> pgZhdqHgjMap = new HashMap<>();
        pgZhdqHgjMap.put("supplierCode", "1000015019");
        pgZhdqHgjMap.put("categoryCode", "40001");
        pgZhdqHgjMap.put("startTime", startTime);
        pgZhdqHgjMap.put("endTime", endTime);

        int pgZhdqHgjNum = cJsjService.hgjCount(pgZhdqHgjMap);
        // 平高组合电器烘干间温度
        HashMap<String, Object> pgDlqHgjMap = new HashMap<>();
        pgDlqHgjMap.put("supplierCode", "1000015019");
        pgDlqHgjMap.put("categoryCode", "40002");
        pgDlqHgjMap.put("startTime", startTime);
        pgDlqHgjMap.put("endTime", endTime);
        int pgDlqHgjNum = cJsjService.hgjCount(pgDlqHgjMap);
        // 平芝组合电器外装间
        HashMap<String, Object> pzZhdqWzjMap = new HashMap<>();
        pzZhdqWzjMap.put("supplierCode", "1000014563");
        pzZhdqWzjMap.put("categoryCode", "40001");
        pzZhdqWzjMap.put("startTime", startTime);
        pzZhdqWzjMap.put("endTime", endTime);
        int pzZhdqWzjNum = cJsjService.wzjCount(pzZhdqWzjMap);
        // 平高组合电器外装间
        HashMap<String, Object> pgZhdqWzjMap = new HashMap<>();
        pgZhdqWzjMap.put("supplierCode", "1000015019");
        pgZhdqWzjMap.put("categoryCode", "40001");
        pgZhdqWzjMap.put("startTime", startTime);
        pgZhdqWzjMap.put("endTime", endTime);
        int pgZhdqWzjNum = cJsjService.wzjCount(pgZhdqWzjMap);
        // 平高断路器外装间
        HashMap<String, Object> pgDlqWzjMap = new HashMap<>();
        pgDlqWzjMap.put("supplierCode", "1000015019");
        pgDlqWzjMap.put("categoryCode", "40002");
        pgDlqWzjMap.put("startTime", startTime);
        pgDlqWzjMap.put("endTime", endTime);
        int pgDlqWzjNum = cJsjService.wzjCount(pgDlqWzjMap);
        // 平芝组合电器内装间
        HashMap<String, Object> pzZhdqNzjMap = new HashMap<>();
        pzZhdqNzjMap.put("supplierCode", "1000014563");
        pzZhdqNzjMap.put("categoryCode", "40001");
        pzZhdqNzjMap.put("startTime", startTime);
        pzZhdqNzjMap.put("endTime", endTime);
        int pzZhdqNzjNum = cJsjService.nzjCount(pzZhdqNzjMap);
        // 平高组合电器内装间
        HashMap<String, Object> pgZhdqNzjMap = new HashMap<>();
        pgZhdqNzjMap.put("supplierCode", "1000015019");
        pgZhdqNzjMap.put("categoryCode", "40001");
        pgZhdqNzjMap.put("startTime", startTime);
        pgZhdqNzjMap.put("endTime", endTime);
        int pgZhdqNzjNum = cJsjService.nzjCount(pgZhdqNzjMap);
        // 平高断路器内装间
        HashMap<String, Object> pgDlqNzjMap = new HashMap<>();
        pgDlqNzjMap.put("supplierCode", "1000015019");
        pgDlqNzjMap.put("categoryCode", "40002");
        pgDlqNzjMap.put("startTime", startTime);
        pgDlqNzjMap.put("endTime", endTime);
        int pgDlqNzjNum = cJsjService.nzjCount(pgDlqNzjMap);

        Map<String, Number[]> data = new HashMap<>();
        data.put("hgj", new Number[]{pzZhdqHgjNum, pgDlqHgjNum, pgZhdqHgjNum});
        data.put("wzj", new Number[]{pzZhdqWzjNum, pgDlqWzjNum, pgZhdqWzjNum});
        data.put("nzj", new Number[]{pzZhdqNzjNum, pgDlqNzjNum, pgZhdqNzjNum});
        return Result.ok(data);
    }

    /**
     * 查询试验数据
     *
     * @param startTime 起始日期
     * @param endTime   结束日期
     * @return
     */
    @ApiOperation("查询试验数据")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/sysjList")
    public Result<?> SelectSysjList(
            @RequestParam(name = "startTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(name = "endTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        HashMap<String, Object> pzZhdqMap = new HashMap<>();
        pzZhdqMap.put("supplierCode", "1000014563");
        pzZhdqMap.put("categoryCode", "40001");
        pzZhdqMap.put("startTime", startTime);
        pzZhdqMap.put("endTime", endTime);
        // 平芝机械特性
        int pzZhdqJxtxNum = sysjService.jxtxCount(pzZhdqMap);
        int pzZhdqLdcjsyNum = sysjService.ldcjsyCount(pzZhdqMap);
        int pzZhdqGpnsdyNum = sysjService.gpnsdyCount(pzZhdqMap);
        int pzZhdqJbfdNum = sysjService.jbfdCount(pzZhdqMap);

        HashMap<String, Object> pgZhdqMap = new HashMap<>();
        pgZhdqMap.put("supplierCode", "1000015019");
        pgZhdqMap.put("categoryCode", "40001");
        pgZhdqMap.put("startTime", startTime);
        pgZhdqMap.put("endTime", endTime);
        int pgZhdqJxtxNum = sysjService.jxtxCount(pgZhdqMap);
        int pgZhdqLdcjsyNum = sysjService.ldcjsyCount(pgZhdqMap);
        int pgZhdqGpnsdyNum = sysjService.gpnsdyCount(pgZhdqMap);
        int pgZhdqJbfdNum = sysjService.jbfdCount(pgZhdqMap);

        HashMap<String, Object> pgDlqMap = new HashMap<>();
        pgDlqMap.put("supplierCode", "1000015019");
        pgDlqMap.put("categoryCode", "40002");
        pgDlqMap.put("startTime", startTime);
        pgDlqMap.put("endTime", endTime);
        int pgDlqJxtxNum = sysjService.jxtxCount(pgDlqMap);
        int pgDlqLdcjsyNum = sysjService.ldcjsyCount(pgDlqMap);
        int pgDlqGpnsdyNum = sysjService.gpnsdyCount(pgDlqMap);
        int pgDlqJbfdNum = sysjService.jbfdCount(pgDlqMap);

        Map<String, Number[]> data = new HashMap<>();
        data.put("jxtx", new Number[]{pzZhdqJxtxNum, pgDlqJxtxNum, pgZhdqJxtxNum});
        data.put("ldcjsy", new Number[]{pzZhdqLdcjsyNum, pgDlqLdcjsyNum, pgZhdqLdcjsyNum});
        data.put("gpnssy", new Number[]{pzZhdqGpnsdyNum, pgDlqGpnsdyNum, pgZhdqGpnsdyNum});
        data.put("jbfd", new Number[]{pzZhdqJbfdNum, pgDlqJbfdNum, pgZhdqJbfdNum});
        return Result.ok(data);
    }

    /**
     * 查询推送订单条数
     *
     * @param startTime 起始日期
     * @param endTime   结束日期
     * @return
     */
    @ApiOperation("查询同步订单条数")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/acquisitionOrdersCount")
    public Result<?> selectAcquisitionOrdersCount(
            @RequestParam(name = "startTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(name = "endTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

        Date acquisitionDateStart = startTime;
        Date acquisitionDateEnd = endTime;
        if (startTime == null && endTime == null) {
            acquisitionDateStart = DateFormat.String2Date2(year + "-" + month + "-1 " + "00:00:00");
            acquisitionDateEnd = DateFormat.String2Date2(year + "-" + month + "-" + day + " 23:59:59");
        }

        EipDataAcquisitionInfo pzEipDataAquisitionInfo = new EipDataAcquisitionInfo();
        pzEipDataAquisitionInfo.setSupplierCode("1000014563");
        pzEipDataAquisitionInfo.setAcquisitionDateStart(acquisitionDateStart);
        pzEipDataAquisitionInfo.setAcquisitionDateEnd(acquisitionDateEnd);

        EipDataAcquisitionInfo pgEipDataAquisitionInfo = new EipDataAcquisitionInfo();
        pgEipDataAquisitionInfo.setSupplierCode("1000015019");
        pgEipDataAquisitionInfo.setAcquisitionDateStart(acquisitionDateStart);
        pgEipDataAquisitionInfo.setAcquisitionDateEnd(acquisitionDateEnd);

        String pzAcquisitionNum = eipDataAcquisitionInfoService.selectSuccessNum(pzEipDataAquisitionInfo);
        int pzAcquisitionNum1 = pzAcquisitionNum == null ? 0 : Integer.parseInt(pzAcquisitionNum);
        String pgAcquisitionNum = eipDataAcquisitionInfoService.selectSuccessNum(pgEipDataAquisitionInfo);
        int pgAcquisitionNum1 = pgAcquisitionNum == null ? 0 : Integer.parseInt(pgAcquisitionNum);
        Map<String, Number[]> data = new HashMap<>();
        data.put("sjcjsl", new Number[]{pzAcquisitionNum1, pgAcquisitionNum1});
        return Result.ok(data);
    }

    /**
     * 查询同步订单条数
     *
     * @param startTime 起始日期
     * @param endTime   结束日期
     * @return 是否成功
     */
    @ApiOperation("查询同步订单条数")
    @AutoLog(value = "IndexSerach")
    @RequestMapping(method = RequestMethod.GET, value = "/synchronizationOrdersCount")
    public Result<?> selectSynchronizationOrdersCountt(
            @RequestParam(name = "startTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(name = "endTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        Date synchronizationDateStart = startTime;
        Date synchronizationDateEnd = endTime;
        if (startTime == null && endTime == null) {
            synchronizationDateStart = DateFormat.String2Date2(year + "-" + month + "-1 " + "00:00:00");
            synchronizationDateEnd = DateFormat.String2Date2(year + "-" + month + "-" + day + " 23:59:59");
        }

        EipDataSynchronizationInfo pzSgccEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pzSgccEipDataSynchronizationInfo.setPurchaserCode("SGCC");
        pzSgccEipDataSynchronizationInfo.setSupplierCode("1000014563");
        pzSgccEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pzSgccEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        EipDataSynchronizationInfo pgSgccEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pgSgccEipDataSynchronizationInfo.setPurchaserCode("SGCC");
        pgSgccEipDataSynchronizationInfo.setSupplierCode("1000015019");
        pgSgccEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pgSgccEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        EipDataSynchronizationInfo pzCsgSgccEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pzCsgSgccEipDataSynchronizationInfo.setPurchaserCode("CSG");
        pzCsgSgccEipDataSynchronizationInfo.setSupplierCode("1000014563");
        pzCsgSgccEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pzCsgSgccEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        EipDataSynchronizationInfo pgCsgSgccEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pgCsgSgccEipDataSynchronizationInfo.setPurchaserCode("CSG");
        pgCsgSgccEipDataSynchronizationInfo.setSupplierCode("1000015019");
        pgCsgSgccEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pgCsgSgccEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        EipDataSynchronizationInfo pzQtEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pzQtEipDataSynchronizationInfo.setPurchaserCode("QT");
        pzQtEipDataSynchronizationInfo.setSupplierCode("1000014563");
        pzQtEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pzQtEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        EipDataSynchronizationInfo pgQtEipDataSynchronizationInfo = new EipDataSynchronizationInfo();
        pgQtEipDataSynchronizationInfo.setPurchaserCode("QT");
        pgQtEipDataSynchronizationInfo.setSupplierCode("1000015019");
        pgQtEipDataSynchronizationInfo.setSynchronizationDateStart(synchronizationDateStart);
        pgQtEipDataSynchronizationInfo.setSynchronizationDateEnd(synchronizationDateEnd);

        String pzSgccAcquisitionNum = eipDataSynchronizationInfoService
                .selectSuccessNum(pzSgccEipDataSynchronizationInfo);
        int pzSgccAcquisitionNum1 = pzSgccAcquisitionNum == null ? 0 : Integer.parseInt(pzSgccAcquisitionNum);
        String pgSgccAcquisitionNum = eipDataSynchronizationInfoService
                .selectSuccessNum(pgSgccEipDataSynchronizationInfo);
        int pgSgccAcquisitionNum1 = pgSgccAcquisitionNum == null ? 0 : Integer.parseInt(pgSgccAcquisitionNum);
        String pzCsgAcquisitionNum = eipDataSynchronizationInfoService
                .selectSuccessNum(pzCsgSgccEipDataSynchronizationInfo);
        int pzCsgAcquisitionNum1 = pzCsgAcquisitionNum == null ? 0 : Integer.parseInt(pzCsgAcquisitionNum);
        String pgCsgAcquisitionNum = eipDataSynchronizationInfoService
                .selectSuccessNum(pgCsgSgccEipDataSynchronizationInfo);
        int pgCsgAcquisitionNum1 = pgCsgAcquisitionNum == null ? 0 : Integer.parseInt(pgCsgAcquisitionNum);
        String pzQtAcquisitionNum = eipDataSynchronizationInfoService.selectSuccessNum(pzQtEipDataSynchronizationInfo);
        int pzQtAcquisitionNum1 = pzQtAcquisitionNum == null ? 0 : Integer.parseInt(pzQtAcquisitionNum);
        String pgQtAcquisitionNum = eipDataSynchronizationInfoService.selectSuccessNum(pgQtEipDataSynchronizationInfo);
        int pgQtAcquisitionNum1 = pgQtAcquisitionNum == null ? 0 : Integer.parseInt(pgQtAcquisitionNum);

        Map<String, Number[]> data = new HashMap<>();
        data.put("gwtssl", new Number[]{pzSgccAcquisitionNum1, pgSgccAcquisitionNum1});
        data.put("nwtssl", new Number[]{pzCsgAcquisitionNum1, pgCsgAcquisitionNum1});
        data.put("qttssl", new Number[]{pzQtAcquisitionNum1, pgQtAcquisitionNum1});
        return Result.ok(data);
    }

}